<?php
class Admin_TemplatesController extends Zend_Controller_Action
{ 	    
	 /**
     * This action is the home page of the website
     *
     */
    public function indexAction()
    {
        $this->view->title = 'Welcome to Wmp titre!';
        $this->view->headTitle('Welcome');
		$config = Zend_Registry::get('config');    	
		/**
		 * Browse all the templates that exist in the templates dir
		 * and check if they are all registred in data base.
		 * If not inserts them if they don't exist.
		 */
		$Templates = new Wmp_Model_TemplatesTable();
        $fileSystem_templates ="";    
        if ($handle = opendir($config->path->templates->dir)) 
    	{   			
    		while (false !== ($file = readdir($handle))) 
   			{
        		if ($file != "." && $file != ".." && $file != "default") 
        		{
            		$fileSystem_templates[]=$file;
            		if($Templates->find($file)->count()==0)
            		{				
            			$new_template = array('name'  => $file);
            			$Templates->insert($new_template);            		          			
            		}
        		}
    		}
    		closedir($handle);
		}		
		$this->view->templates=$Templates->fetchAll();
		$this->view->fileSystemTemplates=$fileSystem_templates;        
    }    
}